import {
	BASE_URL,
	REQUEST_SUCCESS
} from './config';

const requestUpload = ({
	url,
	filePath = '',
	data = {},
	headers = {},
	store=undefined
}) => {
	return new Promise((resolve, reject) => {
		let token
		if(store!=undefined){
			token = store.getters['auth/token'];	
		}
		if (uni.getStorageSync('token')) {
			token = uni.getStorageSync('token')
		}
		if(token){
			headers=Object.assign({
				Authorization:'Bearer '+token
			})
		}
		uni.uploadFile({
		  url: BASE_URL + url,
		  filePath,
		  name: 'file',
		  formData: data,
		  header: {
		  	'Content-Type': 'application/json',
		  	...headers
		  },
		  success: (res) => {
		    if (res.statusCode === 200) {
				let resData=res.data
				if(typeof resData ==='string'){
					resData=JSON.parse(resData)
				}
		    	resolve(resData);
		    } else {
		    	reject(new Error(`Response statusCode is ${res.statusCode}`));
		    }
		  },
		  fail: err => reject(err)
		});
	});
};
export default requestUpload;